package com.meritit.portal.SsoSystem.web;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.meritit.portal.SsoSystem.bean.SsoSystem;
import com.meritit.portal.SsoSystem.service.ISsoSystemMgr;
import com.meritit.portal.SsoSystem.service.SsoSystemMgrFactory;
import com.meritit.portal.login.bean.PreferenceBean;
import com.meritit.portal.operlog.bean.TPOperlog;
import com.meritit.portal.operlog.service.OperlogFactory;
import com.meritit.portal.operlog.service.OperlogIntfs;
import com.meritit.portal.util.DateUtils;
import com.meritit.portal.util.Encrypt;
import com.meritit.portal.util.ListContainer;
import com.meritit.portal.util.PageAction;
import com.meritit.portal.util.ParamUtils;
import com.meritit.portal.util.UUIDUtil;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 业务入口action类
 * 公司: 西安美林电子有限责任公司
 * 作者：xuejp，Dec 6, 2011 11:58:00 AM
 * 描述：用于页面请求处理的action类
 * 说明:
 */
public class ASsoSystemConfig extends ActionSupport{
	private static final long serialVersionUID = 7360829235105230894L;
	private static Logger log = Logger.getLogger(ASsoSystemConfig.class);
    private ISsoSystemMgr mgr = SsoSystemMgrFactory.getSsoSystemMgr();
    private OperlogIntfs logMgr = OperlogFactory.getSysLogService(); 
    public static final String REFUSE = "Refuse";
    public static final String EXCEPTION = "Exception";
    
    /**
     * 系统列表
     * @return
     * @throws Exception
     */
	public String configList() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		 try {
            String userNewId = (String) request.getAttribute("userId");
            if (userNewId == null) {
            	userNewId = request.getParameter("userId");
    			if (userNewId == null || "".equals(userNewId)) {
    				return REFUSE;
    			}
    		}
            int currentPageInt = ParamUtils.getIntParameter(request, "currentPage", 1);
            int itemsInPage = PreferenceBean.displayRows;
            String action = ParamUtils.getParameter(request, "pageAction", true);
            if ("".equals(action)) {
                action = PageAction.FIRST.toString();
            }
            int jumpPage = ParamUtils.getIntParameter(request, "jumpPage", 1);
            ListContainer list =  mgr.configList(currentPageInt, itemsInPage, action,
                    jumpPage);
            request.setAttribute("syslist", list);
    		request.setAttribute("userId", userNewId);
        } catch (Exception e) {
            log.error("获取业务系统注册列表出错：" + e.getMessage());
            request.setAttribute("exception", e);
            return EXCEPTION;
        }
        return SUCCESS;
	}

	/**
	 * 预增加
	 * @return
	 * @throws Exception
	 */
	public String preAdd() throws Exception{
		 try {
            HttpServletRequest request = ServletActionContext.getRequest();
            String userNewId = (String) request.getAttribute("userId");
            if(userNewId == null){
            	userNewId = request.getParameter("userId");
    			if(userNewId == null || "".equals(userNewId)){
    				return REFUSE;
    			}
    		}
           List<String> list = mgr.imgList();
           request.setAttribute("imglist", list);
           request.setAttribute("userId", userNewId);
        } catch (Exception e) {
            log.error("初始化业务系统注册页面失败，失败原因："+e.getMessage());
            return EXCEPTION;
        }
        return "add";
	}
	
	/**
	 * 新增
	 * @return
	 * @throws Exception
	 */
	public String add() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response  = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {
            String userNewId = (String) request.getAttribute("userId");
            if (userNewId == null) {
            	userNewId = request.getParameter("userId");
    			if (userNewId == null || "".equals(userNewId)) {
    				return REFUSE;
    			}
    		}
            String shortName = ParamUtils.getParameter(request, "shortName", true);
            String code = ParamUtils.getParameter(request, "code", true);
            String fullName = ParamUtils.getParameter(request, "fullName", true);
            String loginUrl = ParamUtils.getParameter(request, "loginUrl", true);
            String isShow = ParamUtils.getParameter(request, "isShow", true);
            String cbWsEnable = ParamUtils.getParameter(request, "cbWsEnable", true);
            String cbWsUrl = ParamUtils.getParameter(request, "cbWsUrl", true);
            String cbWsNameSpace = ParamUtils.getParameter(request, "cbWsNameSpace", true);
            String summary = ParamUtils.getParameter(request, "summary", true);
            String isPublic = ParamUtils.getParameter(request, "isPublic", true);
            String imageName = ParamUtils.getParameter(request, "imageName", true);
            String zdWsUrl = ParamUtils.getParameter(request, "zdWsUrl", true);
            String zdWsNameSpace = ParamUtils.getParameter(request, "zdWsNameSpace", true);
            SsoSystem system = new SsoSystem();
            system.setId(UUIDUtil.nextCode());
            system.setShortName(shortName);
            system.setLoginUrl(loginUrl);
            system.setFullName(fullName);
            system.setCode(code);
            system.setIsShow(isShow);
            system.setCbWsUrl(cbWsUrl);
            system.setCbWsEnable(cbWsEnable);
            system.setCbWsNameSpace(cbWsNameSpace);
            system.setSummary(summary);
            system.setIsPublic(isPublic);
            system.setImageName(imageName);
            system.setZdWsUrl(zdWsUrl);
            system.setZdWsNameSpace(zdWsNameSpace);
            
            mgr.configAdd(system);
            String currDate = DateUtils.getCurrDateStr();
    	   	String userId = Encrypt.decrypt(currDate, userNewId);
            //添加操作日志
            String timeStr = DateUtils.getCurrDateTimeStr();
            TPOperlog tplog = new TPOperlog();
            tplog.setOperdateD(timeStr);
            tplog.setUsername(userId);
            tplog.setModuleV("业务入口");
            tplog.setFunctionV("业务入口");
            tplog.setStationnameV("系统管理员");
            tplog.setDataV("新增了一个业务系统信息，信息为【简称:" + shortName + "，编码：" + code +
            		"，是否显示：" + isShow + "，登录地址：" + loginUrl +
            		"，是否启用常用业务：" + cbWsEnable + "，常用业务地址：" + cbWsUrl);
            tplog.setHasexceptionV("否");
            tplog.setOperationV("新增业务系统信息");
            try {
                logMgr.saveOperlog(tplog);
            } catch (Exception e) {
                log.error("添加用户操作日志失败", e);
            }
            out.write("{\"status\": \"y\", \"info\": \"业务系统添加成功\"}");
            return null;
        } catch (Exception e) {
            log.error("添加业务系统信息出错：" + e.getMessage());
			out.write("{\"status\": \"n\", \"info\": \"业务系统添加失败\"}");
			return null;
		} finally {
			out.close();
		}
	       
	}
	
	/**
	 * 预修改
	 * @return
	 * @throws Exception
	 */
	public String preUpdate() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
            String userNewId = (String) request.getAttribute("userId");
            if(userNewId == null){
            	userNewId = request.getParameter("userId");
    			if(userNewId == null || "".equals(userNewId)){
    				return REFUSE;
    			}
    		}
            String id = ParamUtils.getParameter(request, "id", true);
            SsoSystem system = mgr.queryById(id);
            List<String> list = mgr.imgList();
            request.setAttribute("imglist", list);
            request.setAttribute("system", system);
	        request.setAttribute("userId", userNewId);
            return "update";
        } catch (Exception e) {
            log.error("初始化业务系统信息修改页面出错：" + e.getMessage());
            request.setAttribute("exception", e);
            return EXCEPTION;
        }
	}
	
	/**
	 * 修改业务系统信息
	 * @return
	 * @throws Exception
	 */
	public String update() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response  = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {
			String userNewId = (String) request.getAttribute("userId");
            if(userNewId == null){
            	userNewId = request.getParameter("userId");
    			if(userNewId == null || "".equals(userNewId)){
    				return REFUSE;
    			}
    		}
			String id = ParamUtils.getParameter(request, "id", true);
            String shortName = ParamUtils.getParameter(request, "shortName", true);
            String code = ParamUtils.getParameter(request, "code", true);
            String fullName = ParamUtils.getParameter(request, "fullName", true);
            String loginUrl = ParamUtils.getParameter(request, "loginUrl", true);
            String isShow = ParamUtils.getParameter(request, "isShow", true);
            String cbWsEnable = ParamUtils.getParameter(request, "cbWsEnable", true);
            String cbWsUrl = ParamUtils.getParameter(request, "cbWsUrl", true);
            String cbWsNameSpace = ParamUtils.getParameter(request, "cbWsNameSpace", true);
            String summary = ParamUtils.getParameter(request, "summary", true);
            String isPublic = ParamUtils.getParameter(request, "isPublic", true);
            String imageName = ParamUtils.getParameter(request, "imageName", true);
            String zdWsUrl = ParamUtils.getParameter(request, "zdWsUrl", true);
            String zdWsNameSpace = ParamUtils.getParameter(request, "zdWsNameSpace", true);
            SsoSystem system = new SsoSystem();
            system.setId(id);
            system.setShortName(shortName);
            system.setLoginUrl(loginUrl);
            system.setFullName(fullName);
            system.setCode(code);
            system.setIsShow(isShow);
            system.setCbWsUrl(cbWsUrl);
            system.setCbWsEnable(cbWsEnable);
            system.setCbWsNameSpace(cbWsNameSpace);
            system.setSummary(summary);
            system.setIsPublic(isPublic);
            system.setImageName(imageName);
            system.setZdWsUrl(zdWsUrl);
            system.setZdWsNameSpace(zdWsNameSpace);
			mgr.configUpdate(system);
			String currDate = DateUtils.getCurrDateStr();
			String userId = Encrypt.decrypt(currDate, userNewId);
			//添加操作日志
            String timeStr = DateUtils.getCurrDateTimeStr();
            TPOperlog tplog = new TPOperlog();
            tplog.setOperdateD(timeStr);
            tplog.setUsername(userId);
            tplog.setModuleV("业务入口");
            tplog.setFunctionV("业务入口");
            tplog.setStationnameV("系统管理员");
            tplog.setDataV("修改了一个业务系统信息，信息为【简称:" + shortName + "，编码：" + code +
            		"，是否显示：" + isShow + "，登录地址：" + loginUrl +
            		"，是否启用常用业务：" + cbWsEnable + "，常用业务地址：" + cbWsUrl);
            tplog.setHasexceptionV("否");
            tplog.setOperationV("修改业务系统信息");
            try {
                logMgr.saveOperlog(tplog);
            } catch (Exception e) {
                log.error("添加用户操作日志失败", e);
            }
            out.write("{\"status\": \"y\", \"info\": \"业务系统修改成功\"}");
            return null;
		} catch (Exception e) {
			log.error("修改业务系统信息出错："+e.getMessage());
			out.write("{\"status\": \"n\", \"info\": \"业务系统修改失败\"}");
			return null;
		} finally {
			out.close();
		}
	}
	
	/**
	 * 删除业务系统信息
	 * @return
	 * @throws Exception
	 */
	public String del() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response  = ServletActionContext.getResponse();
		try {
			String userNewId = (String) request.getAttribute("userId");
            if (userNewId == null) {
            	userNewId = request.getParameter("userId");
    			if (userNewId == null || "".equals(userNewId)) {
    				return REFUSE;
    			}
    		}
			String id = ParamUtils.getParameter(request, "id", true);
			mgr.configDel(id);
			response.setContentType("text/html;charset=UTF-8");
            response.getWriter().write("ok");
			return null;
		} catch (Exception e) {
			log.error("删除业务系统信息出错：" + e.getMessage());
			response.setContentType("text/html;charset=UTF-8");
            response.getWriter().write(e.getMessage());
			return null;
		}
	}
	
	/**
	 * 校验业务系统编码是否重复
	 * @return
	 * @throws Exception
	 */
	public String check() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {
			String beId = ParamUtils.getParameter(request, "beId", false);
			String beCode = ParamUtils.getParameter(request, "param", true);
			int res = mgr.checkAppCode(beCode, beId);
			if (res > 0) {
				out.write("业务系统编码已经存在!");
			} else {
				out.write("y");
			}
			return null;
		} catch (Exception e) {
			log.error("校验业务系统编码是否重复出错：" + e.getMessage());
			out.write("校验业务系统编码是否重复出错");
			return null;
		}
	}
	
}
